package cn.iocoder.an.module.business.service.auditarea;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.an.module.system.dal.dataobject.dept.DeptDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.an.module.business.controller.admin.auditarea.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.auditarea.AuditAreaDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.business.dal.mysql.auditarea.AuditAreaMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.business.enums.ErrorCodeConstants.*;

/**
 * 分层审核区域设置 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class AuditAreaServiceImpl implements AuditAreaService {

    @Resource
    private AuditAreaMapper auditAreaMapper;

    @Override
    public Long createAuditArea(AuditAreaSaveReqVO createReqVO) {
        // 插入
        List<DeptDO> deptList = createReqVO.getDeptList();

        List<AuditAreaDO> addList = new ArrayList<>();
        for (DeptDO deptDO : deptList) {
            AuditAreaDO auditArea = new AuditAreaDO();
            auditArea.setDeptId(deptDO.getId());
            auditArea.setParentDeptId(deptDO.getParentId());
            auditArea.setDutyUserId(deptDO.getLeaderUserId());
            auditArea.setDeptName(deptDO.getName());
            addList.add(auditArea);
        }
        if (!CollUtil.isEmpty(addList)) {
            auditAreaMapper.insertBatch(addList);
        }
        return 1L;
    }

    @Override
    public void updateAuditArea(AuditAreaSaveReqVO updateReqVO) {
        // 校验存在
        validateAuditAreaExists(updateReqVO.getId());
        // 更新
        AuditAreaDO updateObj = BeanUtils.toBean(updateReqVO, AuditAreaDO.class);
        auditAreaMapper.updateById(updateObj);
    }

    @Override
    public void deleteAuditArea(Long id) {
        // 校验存在
        validateAuditAreaExists(id);
        // 删除
        auditAreaMapper.deleteById(id);
    }

    @Override
    public void deleteAuditAreaListByIds(List<Long> ids) {
        // 删除
        auditAreaMapper.deleteByIds(ids);
    }


    private void validateAuditAreaExists(Long id) {
        if (auditAreaMapper.selectById(id) == null) {
            throw exception(AUDIT_AREA_NOT_EXISTS);
        }
    }

    @Override
    public AuditAreaDO getAuditArea(Long id) {
        return auditAreaMapper.selectById(id);
    }

    @Override
    public PageResult<AuditAreaDO> getAuditAreaPage(AuditAreaPageReqVO pageReqVO) {
        return auditAreaMapper.selectPage(pageReqVO);
    }

    @Override
    public List<Long> getAuditAreaDeptIds() {
        LambdaQueryWrapperX<AuditAreaDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.isNotNull(AuditAreaDO::getDeptId)
                .select(AuditAreaDO::getDeptId); // 只查询 deptId 字段

        List<AuditAreaDO> auditAreaDOS = auditAreaMapper.selectList(queryWrapperX);

        // 提取 deptId 并转换为 List<Long>
        return auditAreaDOS.stream()
                .map(AuditAreaDO::getDeptId)
                .collect(Collectors.toList());
    }

}